package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.dao.UserProjectDao;
import com.rstrulak.model.Project;
import com.rstrulak.model.User;
import com.rstrulak.model.UserProject;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("assignUserToProjectBean")
@Scope("request")
public class AssignUserToProjectBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private UserProjectDao userProjectDao;

	@Autowired
	UserDao userDao;

	private List<String> assignedUsers;

	private Map<String, Long> allUsersMap;

	private Long projectId;

	private Project projectById;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		
		projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);

		List<User> allUsersList = new ArrayList<User>();

		List<Long> userIdList = getAlreadyAssignedUsers();

		if (!userIdList.isEmpty()) {
			allUsersList = userDao.getAllUsersExceptUser(userIdList);
		} else {
			List<Long> allUserListExceptLogged = new ArrayList<Long>();
			allUserListExceptLogged.add(JSFUtils.getLoggedUser().getId());
			allUsersList = userDao.getAllUsersExceptUser(allUserListExceptLogged);
		}

		allUsersMap = new HashMap<String, Long>();

		for (User user : allUsersList) {
			allUsersMap.put(user.getFirstName() + " " + user.getLastName(), user.getId());
		}

	}

	@PreDestroy
	public void destroy() {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
	}

	private List<Long> getAlreadyAssignedUsers() {
		projectById = projectDao.getById(projectId);

		List<Long> userIdList = new ArrayList<Long>();

		List<UserProject> allUserListByProject = userProjectDao.getAllUserListByProject(projectById);
		for (UserProject userProject : allUserListByProject) {
			userIdList.add(userProject.getUser().getId());
		}
		return userIdList;
	}

	public Long getProjectId() {
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	public Project getProjectById() {
		return projectById;
	}

	public void setProjectById(Project projectById) {
		this.projectById = projectById;
	}

	public Map<String, Long> getAllUsersMap() {
		return allUsersMap;
	}

	public void setAllUsersMap(Map<String, Long> allUsersMap) {
		this.allUsersMap = allUsersMap;
	}

	public List<String> getAssignedUsers() {
		return assignedUsers;
	}

	public void setAssignedUsers(List<String> assignedUsers) {
		this.assignedUsers = assignedUsers;
	}

	public void assignUsers() throws ServletException, IOException {
		for (String userId : assignedUsers) {
			UserProject userProject = new UserProject();
			userProject.setUser(userDao.getById(new Long(userId)));
			userProject.setProject(projectById);
			userProjectDao.save(userProject);
		}

		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void cancelAssignUsers() throws ServletException, IOException {
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

}
